'use strict';

angular.module('fm.controllers', ['ngRoute','ngSanitize','ngAnimate','mediaPlayer'])

// .config(function ($interpolateProvider) {
//   $interpolateProvider.startSymbol('[[');
//   $interpolateProvider.endSymbol(']]');
// })

.run(['$location', '$rootScope','$route', function($location, $rootScope,$route) {

    // 初始菜单
    $rootScope.$on('$routeChangeSuccess', function (currentRoute, previousRoute) {
    	$rootScope.title = $route.current.title;
    });

    // helper function to seek to a percentage
    $rootScope.seekPercentage = function ($event) {
        var percentage = ($event.offsetX / $event.target.offsetWidth);
        if (percentage <= 1) {
            return percentage;
        } else {
            return 0;
        }
    };

}])

// 头部
.controller('HeaderCtrl', ['$scope','$rootScope','$location', function ($scope,$rootScope,$location) {
    $scope.isActive = function (viewLocation) { 
        return viewLocation === $location.path();
    };
}])

.controller('LoginCtrl', ['$scope', function ($scope){
    
}])

.controller('FmCtrl', ['$scope','$rootScope','Channel','People', function ($scope,$rootScope,Channel,People){
    $scope.playList = [];
    $scope.cl = Channel.one();
    $scope.pl = People.one(0);

    $scope.pl.$promise.then(function(result) {
        for (var i = 0; i < result.song.length; i++) {
            var song = result.song[i];
                song.src = song.url.replace(/http:\/\/mr[0-9]{1,}.douban.com/g,'/player');
                song.type = 'audio/mp3';
                song.duration = song.length;
                delete(song.url);
            $scope.playList.push(song);
        };
        $scope.showPlayer = true;
    });

    $scope.peopleList = function(cid) {
        $scope.pl = People.one(cid);
        $scope.pl.$promise.then(function(result) {
            for (var i = 0; i < result.song.length; i++) {
                var song = result.song[i];
                    song.src = song.url.replace('http://mr?.douban.com','/player');
                    song.type = 'audio/mp3';
                    song.duration = song.length;
                    delete(song.url);
                $scope.playList.push(song);
            };
        });
    }

    $scope.toPlay = function(song){
        $rootScope.title = song.title;
        song.src = song.url.replace('http://mr?.douban.com','/player');
        song.type = 'audio/mp3';
        song.duration = song.length;
        delete(song.url);
        $scope.playList.push(song);
    }

}])

;
